import dpkt
import socket

file = './openflow6.pcap'
with open(file, 'rb') as fr:
    pcap = dpkt.pcap.Reader(fr)
    for timestamp, buffer in pcap:
        ethernet = dpkt.ethernet.Ethernet(buffer)
        # 我们仅需要TCP的包
        if not isinstance(ethernet.data, dpkt.ip.IP):
            continue
        ip = ethernet.data
        print('ip:', ip)
        if not isinstance(ip.data, dpkt.tcp.TCP):
            continue
        tcp = ip.data
        print('tcp', tcp)
        # 过滤掉内容为空的包
        if len(tcp.data) == 0:
            continue
        # 发送方的IP
        src = socket.inet_ntoa(ip.src)
        print('src:', src)
        # 接收方的IP
        dst = socket.inet_ntoa(ip.dst)
        print('dst', dst)
        # 报文内容（byte数组）
        byteArray = tcp.data
        # print(byteArray)
        # TODO 根据自定义的协议内容，解析bytes数组


def get_msg(msg_name):
    return msg.XXX;